// server.js
const express = require("express");
const app = express();
const path = require("path");
const http = require("http");
const io = require("socket.io");
const httpPort = 10003;
app.get("/", function (req, res) {
  res.send("启动成功：" + httpPort);
  //   res.sendFile(path.join(__dirname, "index.html"));
});
let server = http.createServer(app);

// 建立websocket
let wsServer = io(server, { cors: { origin: "*" } });
wsServer.on("connection", sock => {
  sock.on("aaa", function (a, b) {
    console.log(a);
    console.log(b);
    console.log(arguments);
  }); // 'aaa'事件名与前台的一致

  setInterval(function () {
    sock.emit("bbb", "服务器发来的数据"); // name -> 'bbb' 要与前台的 name 保持一致
  }, 2000);
});
server.listen(httpPort);
console.log("localhost:" + httpPort);
